package com.spring.mybatis.anno.service.impl;


import java.util.List;


import javax.annotation.Resource;

import com.spring.mybatis.anno.dao.CarDao;
import com.spring.mybatis.anno.dao.UserDao;
import com.spring.mybatis.anno.po.Car;
import com.spring.mybatis.anno.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spring.mybatis.anno.service.UserService;

@Service("userService")
@Transactional(rollbackFor = Throwable.class)
public class UserServiceImpl implements UserService {


  @Resource
  private UserDao userDao;

  @Autowired
  private CarDao carDao;

  @Override
  @Transactional(readOnly = true)
  public User selectById(String id) {
    return userDao.selectById(id);
  }

  @Override
  @Transactional(readOnly = true)
  public User selectByUserName(String username) {
    return userDao.selectByUserName(username);
  }

  @Override
  @Transactional(readOnly = true)
  public List<User> selectAll() {
    return userDao.selectAll();
  }

  @Override
  public Long insert(User user) {
    userDao.insert(user);
    return user.getId();
  }

  @Override
  public boolean update(User user) {
    return userDao.update(user) == 1;
  }

  @Override
  public boolean delete(String id) {
    return userDao.deleteById(id) == 1;
  }

  @Override
  public void saveTransactionDb() {
    User user = new User();
    user.setPassword("kkk");
    user.setUsername("tome");
    userDao.insert(user);
    System.out.println(5/0);
    Car car = new Car();
    car.setName("yyy");
    car.setColor("red");
    carDao.insert(car);
  }
}
